import java.io.File;
import java.util.ArrayList;

import components.Component;
import components.InputXMLDocument;

import managers.RuleManager;
import managers.SemanticsManager;
import managers.XMLManager;
import rules.Rule;
import rules.RuleMatcher;
import rules.TagPair;

public class TagalogEngine {

	public static void main(String[] args) {
		
		final String textName = "test1";
		final String filePath = "Rules\\Test\\"+textName+".xml";
		
		ArrayList<Rule> rules = new ArrayList<Rule>();
		ArrayList<Component> samps = new ArrayList<Component>();
		
		int i, n;
			
		i = 0;
		n = 0;
		
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 1A.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 1B.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 1C.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 2.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 3A.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 3B.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 3C.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 3D.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 4A.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 4B.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 4C.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 5A.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 5B.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 5C.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 7.xml")));
		rules.add(RuleManager.readRuleFromFile(new File("Rules\\Tagalog\\rule 8.xml")));

		InputXMLDocument docu = SemanticsManager.readSemanticsDocumentFromFile(new File(filePath));
		for(Component comp : docu.getClauses()){
			samps.add(comp);
			n++;
		}
		
		for(i = 0; i < n; ++i){
			System.out.println("Input:");
			RuleMatcher.printComp(samps.get(i));
			System.out.println();
			
			System.out.println("Checking and Applying rules:");
			for(Rule rule : rules)
				if(rule.checkAndApply(samps.get(i)))
					System.out.println(rule.getName() + " applied.");
			System.out.println();
			
			System.out.println("Output:");
			RuleMatcher.printComp(samps.get(i));
			System.out.println();
			
			XMLManager.getInstance().writeToXML("Rules\\Output\\clause"+i+".xml", samps.get(i).generateXMLElement());
			System.out.println("========================================\n");
		}
		docu.setClauses(samps);
		XMLManager.getInstance().writeToXML("Rules\\Output\\"+textName+"generated.xml", docu.generateXMLCopy());
	}
}